package cn.csc.module.pay.controller.admin.funddetailsinfo;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.pay.controller.admin.funddetailsinfo.vo.*;
import cn.csc.module.pay.convert.funddetailsinfo.PayFundDetailsInfoConvert;
import cn.csc.module.pay.dal.dataobject.funddetailsinfo.PayFundDetailsInfoDO;
import cn.csc.module.pay.service.funddetailsinfo.PayFundDetailsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 用户资金提现分账明细")
@RestController
@RequestMapping("/pay/fund-details-info")
@Validated
public class PayFundDetailsInfoController {

    @Resource
    private PayFundDetailsInfoService fundDetailsInfoService;

    @PostMapping("/create")
    @ApiOperation("创建用户资金提现分账明细")
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:create')")
    public CommonResult<BigInteger> createFundDetailsInfo(@Valid @RequestBody PayFundDetailsInfoCreateReqVO createReqVO) {
        return success(fundDetailsInfoService.createFundDetailsInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户资金提现分账明细")
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:update')")
    public CommonResult<Boolean> updateFundDetailsInfo(@Valid @RequestBody PayFundDetailsInfoUpdateReqVO updateReqVO) {
        fundDetailsInfoService.updateFundDetailsInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户资金提现分账明细")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:delete')")
    public CommonResult<Boolean> deleteFundDetailsInfo(@RequestParam("id") BigInteger id) {
        fundDetailsInfoService.deleteFundDetailsInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户资金提现分账明细")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:query')")
    public CommonResult<PayFundDetailsInfoRespVO> getFundDetailsInfo(@RequestParam("id") BigInteger id) {
        PayFundDetailsInfoDO fundDetailsInfo = fundDetailsInfoService.getFundDetailsInfo(id);
        return success(PayFundDetailsInfoConvert.INSTANCE.convert(fundDetailsInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户资金提现分账明细列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:query')")
    public CommonResult<List<PayFundDetailsInfoRespVO>> getFundDetailsInfoList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<PayFundDetailsInfoDO> list = fundDetailsInfoService.getFundDetailsInfoList(ids);
        return success(PayFundDetailsInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户资金提现分账明细分页")
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:query')")
    public CommonResult<PageResult<PayFundDetailsInfoRespVO>> getFundDetailsInfoPage(@Valid PayFundDetailsInfoPageReqVO pageVO) {
        PageResult<PayFundDetailsInfoDO> pageResult = fundDetailsInfoService.getFundDetailsInfoPage(pageVO);
        return success(PayFundDetailsInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户资金提现分账明细 Excel")
    @PreAuthorize("@ss.hasPermission('pay:fund-details-info:export')")
    @OperateLog(type = EXPORT)
    public void exportFundDetailsInfoExcel(@Valid PayFundDetailsInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayFundDetailsInfoDO> list = fundDetailsInfoService.getFundDetailsInfoList(exportReqVO);
        // 导出 Excel
        List<PayFundDetailsInfoExcelVO> datas = PayFundDetailsInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户资金提现分账明细.xls", "数据", PayFundDetailsInfoExcelVO.class, datas);
    }

}
